package cn.jimmie.learn.plugin;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import cn.jimmie.learn.pbc.base.BaseFragment;
import cn.jimmie.learn.pbc.resource.ShareInstance;
import cn.jimmie.learn.sdk.OnSdkLoginListener;
import cn.jimmie.learn.support.JniUtils;

/**
 * function :
 *
 * <p> Create Time:  2019年02月14日 14:48 </p>
 * <p> @author Jimmie.Qian  </p>
 */
public class LoginFragment extends BaseFragment {

    public LoginFragment() {
    }

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.plugin_fragment_login, container, false);
        final EditText usernameEditor = root.findViewById(R.id.user_name);
        final EditText passwordEditor = root.findViewById(R.id.password);
        Button login = root.findViewById(R.id.login);
        login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String username = usernameEditor.getText().toString();
                String password = passwordEditor.getText().toString();

                if (username.isEmpty() || password.isEmpty()) {
                    System.out.println("插件内部classLoader = " + JniUtils.class.getClassLoader());
                    Toast.makeText(mContext, JniUtils.sayHello(), Toast.LENGTH_SHORT).show();
//                    Toast.makeText(getContext(), "用户名或者密码未填写", Toast.LENGTH_SHORT).show();
                    return;
                }

                OnSdkLoginListener listener = ShareInstance.instance().getSdkLoginListener();
                if (listener != null)
//                    listener = null;
                    listener.onLoginFinished(true, "{username:" + username + ",password:" + password + "}");
                getActivity().finish();
            }
        });

        return root;
    }
}
